/*
package cn.myzf.netty.client.ttest.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
import io.netty.handler.codec.http.websocketx.WebSocketVersion;
import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler;
import io.netty.handler.timeout.IdleStateHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;

*/
/**
 * netty客户端
 *
 * @author lenovo
 *//*


public class NettyClient {
    public Logger log = LoggerFactory.getLogger(this.getClass());

    private EventLoopGroup loop = new NioEventLoopGroup();

    public NettyClient() throws URISyntaxException {
    }

    public void run() throws Exception {
        try {
            doConnect(new Bootstrap(), loop);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    URI uri = new URI("ws://127.0.0.1:8080/websocket");


    public Bootstrap doConnect(Bootstrap bootstrap, EventLoopGroup eventLoopGroup) {
        try {
            final WebSocketClientHandler handler =
                    new WebSocketClientHandler(
                            WebSocketClientHandshakerFactory.newHandshaker(
                                    uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders())
                    );
            if (bootstrap != null) {
                bootstrap.group(eventLoopGroup);
                bootstrap.channel(NioSocketChannel.class);
                bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
                bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    public void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline p = ch.pipeline();

                        p.addLast("idleStateHandler", new IdleStateHandler(0
                                , 5, 0, TimeUnit.SECONDS));

//						p.addLast("idleTimeoutHandler", new HeartHandler(NettyClient.this));
                        p.addLast(new HttpClientCodec(),
                                new HttpObjectAggregator(8192),
                                WebSocketClientCompressionHandler.INSTANCE,
                                handler);

                    }
                });
                bootstrap.remoteAddress(uri.getHost(), uri.getPort());

                ChannelFuture f = bootstrap.connect().addListener((ChannelFuture futureListener) -> {
                    final EventLoop eventLoop = futureListener.channel().eventLoop();
                    if (!futureListener.isSuccess()) {
                        log.warn("连接服务器失败，2s后重新尝试连接！");
                        futureListener.channel().eventLoop().schedule(() -> doConnect(new Bootstrap(), eventLoop), 2, TimeUnit.SECONDS);
                    }
                });
                f.channel().closeFuture().sync();
                //eventLoopGroup.shutdownGracefully();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return bootstrap;
    }

    public static void main(String[] args) throws URISyntaxException {
        NettyClient client = new NettyClient();
        client.doConnect(new Bootstrap(), client.loop);
    }
}
*/
